package com.speedata.blereaderdemo

import android.graphics.Bitmap
import android.graphics.Matrix

object Utils {
    
    /**
     * 字节数组转十六进制字符串
     */
    fun bytesToHexString(bytes: ByteArray): String {
        if (bytes.isEmpty()) return ""
        val hexString = StringBuilder()
        for (b in bytes) {
            val hex = Integer.toHexString(0xFF and b.toInt())
            if (hex.length == 1) {
                hexString.append('0')
            }
            hexString.append(hex)
        }
        return hexString.toString().uppercase()
    }
    
    /**
     * 合并多个字节数组
     */
    fun byteMerger(vararg arrays: ByteArray): ByteArray {
        var totalLength = 0
        for (array in arrays) {
            totalLength += array.size
        }
        
        val result = ByteArray(totalLength)
        var offset = 0
        for (array in arrays) {
            System.arraycopy(array, 0, result, offset, array.size)
            offset += array.size
        }
        
        return result
    }
    
    /**
     * int转两个字节
     */
    fun intToTwoBytes(value: Int): ByteArray {
        return byteArrayOf((value shr 8).toByte(), value.toByte())
    }
    
    /**
     * 图像转换为黑白字节数组（与原方法保持一致）
     */
    fun getBlackWhiteByte(bitmap: Bitmap): ByteArray {
        val width = 416
        val height = 240
        
        // 首先将源位图缩放到目标尺寸
        val scaledBitmap = if (bitmap.width != width || bitmap.height != height) {
            Bitmap.createScaledBitmap(bitmap, width, height, true)
        } else {
            bitmap
        }
        
        // 创建旋转矩阵
        val matrix = Matrix().apply {
            setRotate(90f, width.toFloat(), height.toFloat())
        }
        
        // 创建目标位图并旋转
        val rotatedBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0, width, height, matrix, true)
        
        // 获取像素数据
        val pixels = IntArray(width * height)
        rotatedBitmap.getPixels(pixels, 0, height, 0, 0, height, width)
        
        // 创建结果字节数组
        val result = ByteArray(416 * 30)
        var packedPix: Byte
        
        // 按照原方法的逻辑处理像素
        for (i in 0 until 416) {
            for (j in 0 until 30) {
                packedPix = 0
                for (k in 0 until 8) {
                    packedPix = (packedPix.toInt() shl 1).toByte()
                    
                    if (pixels[i * 240 + j * 8 + k] == 0xffffffff.toInt()) {
                        packedPix = (packedPix.toInt() or 0x01).toByte()
                    }
                }
                result[i * 30 + j] = packedPix
            }
        }
        
        // 回收位图
        rotatedBitmap.recycle()
        if (scaledBitmap != bitmap) {
            scaledBitmap.recycle()
        }
        
        return result
    }

    /** HexToAscii */
    fun hexToAscii(hex: String): String {
        // 移除空格等无效字符
        val cleanHex = hex.filter { it.isLetterOrDigit() }

        return when {
            cleanHex.isEmpty() -> ""
            cleanHex.length % 2 != 0 -> {
                println("警告：十六进制长度应为偶数，已忽略最后一个字符")
                cleanHex.dropLast(1).chunked(2)
                    .map { it.toInt(16).toChar() }
                    .joinToString("")
            }
            else -> cleanHex.chunked(2)
                .map { it.toInt(16).toChar() }
                .joinToString("")
        }
    }

    val whiteScreen: ByteArray = byteArrayOf(
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00,
        0x00
    )
}
